/**
 * @title AttentionTeamAdapter.java
 * @package com.esun.cheetah.view.personal.adapter
 * @description 关注球队
 * @author liugan
 * @date 2012-5-28
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.personal.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.attention.CancelAttenTeam;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.model.personal.attention.AttenTeamInfoChild;
import com.esun.cheetah.model.personal.attention.AttenTeamParent;
import com.esun.util.Tools;

public class EListAdapter extends BaseExpandableListAdapter {
	private List<List<AttenTeamInfoChild>> llTeamR = new ArrayList<List<AttenTeamInfoChild>>();
	private List<AttenTeamParent> attenTeamInfoPa = new ArrayList<AttenTeamParent>();
	private Context mContext;
	private OnRecordCountListener mOnRecordCountListener;
	private boolean isCancelAttention;
	/** 取消关注项的位置 */
	private int canceledGroupPosition;
	private int canceledChildPosition;
	private Handler mHandler;
	private ExpandableListView mListView;

	public EListAdapter(Context context, ExpandableListView listView) {
		this.mContext = context;
		this.mListView = listView;
		mHandler = new AttentionTeamHandler();
	}

	public EListAdapter(List<List<AttenTeamInfoChild>> llTeamR,
			List<AttenTeamParent> attenTeamInfoPa) {
		this.llTeamR = llTeamR;
		this.attenTeamInfoPa = attenTeamInfoPa;
	}

	@Override
	public int getGroupCount() {
		if (attenTeamInfoPa == null) {
			return 0;
		}
		return attenTeamInfoPa.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (llTeamR.get(groupPosition) == null) {
			return 0;
		}
		return llTeamR.get(groupPosition).size();
	}

	@Override
	public AttenTeamParent getGroup(int groupPosition) {
		if (getGroupCount() > 0) {
			return attenTeamInfoPa.get(groupPosition);
		}
		return null;
	}

	@Override
	public AttenTeamInfoChild getChild(int groupPosition, int childPosition) {
		return llTeamR.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		LayoutInflater li = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = li.inflate(R.layout.event_group_item, null);
		ImageView iv = (ImageView) convertView.findViewById(R.id.imGroupPromp);
		TextView tvEvent = (TextView) convertView.findViewById(R.id.tvEvent);
		TextView tvAccount = (TextView) convertView
				.findViewById(R.id.tvAccount);
		tvEvent.setText(attenTeamInfoPa.get(groupPosition).getLeague());
		tvAccount.setText("共" + attenTeamInfoPa.get(groupPosition).getCount()
				+ "支球队");
		if (isExpanded) {
			iv.setBackgroundResource(R.drawable.icon_default_packup);
		} else {
			iv.setBackgroundResource(R.drawable.icon_default_unfold);
		}
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		LayoutInflater li = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = li.inflate(R.layout.atten_team_child_item, null);
		TextView tvName = (TextView) convertView.findViewById(R.id.tvTeamName);
		tvName.setText(llTeamR.get(groupPosition).get(childPosition).getTeam());
		final String teamName = llTeamR.get(groupPosition).get(childPosition)
				.getTeam();
		final String teamId = llTeamR.get(groupPosition).get(childPosition).getTeamid();
		ImageView imLog = (ImageView) convertView.findViewById(R.id.imLogo);
		ImageView ivAttention = (ImageView) convertView
				.findViewById(R.id.imbtIsAttention);
		ivAttention.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!isCancelAttention) {
					isCancelAttention = true;
					canceledGroupPosition = groupPosition;
					canceledChildPosition = childPosition;

					CancelAttenTeam cancelAttenTeam = new CancelAttenTeam(
							mHandler, teamId);
					cancelAttenTeam.startCommunicate();
				}

			}

		});
		String logoUrl = llTeamR.get(groupPosition).get(childPosition)
				.getLogUrl();
		tvName.setText(teamName);
		if (!logoUrl.equals("")) {
			Tools.loadLogo(imLog, logoUrl);
		}
		if (logoUrl.equals(" ")) {
			imLog.setBackgroundDrawable(mContext.getResources().getDrawable(
					R.drawable.img_default_team_logo));
		}
		
		

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void update(List<List<AttenTeamInfoChild>> childList,
			List<AttenTeamParent> parentList) {
		attenTeamInfoPa.clear();
		attenTeamInfoPa.addAll(parentList);
		llTeamR.clear();
		llTeamR.addAll(childList);
		onRecordCount();

	}

	private void onRecordCount() {
		if (mOnRecordCountListener != null) {
			if (attenTeamInfoPa.size() == 0) {
				mOnRecordCountListener.onEmpty();
			} else {
				mOnRecordCountListener.onNotEmpty();
			}
		}
	}

	public void setOnRecordCountListener(OnRecordCountListener l) {
		this.mOnRecordCountListener = l;
	}

	public interface OnRecordCountListener {
		void onEmpty();

		void onNotEmpty();
	}

	class AttentionTeamHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:
				llTeamR.get(canceledGroupPosition)
						.remove(canceledChildPosition);
				int count = Integer.parseInt(attenTeamInfoPa.get(canceledGroupPosition).getCount()) - 1;
				attenTeamInfoPa.get(canceledGroupPosition).setCount(String.valueOf(count));
				if (llTeamR.get(canceledGroupPosition).size() == 0) {
					llTeamR.remove(canceledGroupPosition);
					mListView.collapseGroup(canceledGroupPosition);
					attenTeamInfoPa.remove(canceledGroupPosition);
				}
				onRecordCount();
				EListAdapter.this.notifyDataSetChanged();
				break;
			case Config.COMMUNICATER_EXCEPTION:
			case Config.COMMUNICATER_FAIL:
				Toast.makeText(mContext, "取消失败！", Toast.LENGTH_SHORT).show();
				break;
			default:
			}
			isCancelAttention = false;
		}

	}
}
